﻿using ASPApiDemo.IService;
using ASPApiDemo.Model;
using ASPApiDemo.Repository;
using AutoMapper;

namespace ASPApiDemo.Service
{
    public class BaseService<TEntity, TVo> : IBaseService<TEntity, TVo> where TEntity : class, new()
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<TEntity> baseRepository;
        //IMapper是会被自动注入的
        public BaseService(IMapper mapper, IBaseRepository<TEntity> baseRepository)
        {
            this._mapper = mapper;
            this.baseRepository = baseRepository;
        }
        public async Task<List<TVo>> Query()
        {
            //实例化仓储，从仓储中查询数据拿到实体模型，泛型版本
            var entities = await baseRepository.Query();
            var dtos = _mapper.Map<List<TVo>>(entities);
            return dtos;
        }
    }
}
